﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SellProductsForm
{
    public partial class frmSell : UTLBASEFORM.MDICHILD
    {
        UTLCOMMAND.UTLCommand utcommand = new UTLCOMMAND.UTLCommand();
        DataTable dataPro;
        TSell tSell = new TSell();

       // int NumPro=0;


        public frmSell()
        {
            InitializeComponent();
        }

        private void SetupMenuBar()
        {
            ShowBTN1("Bán", ImageNameCheck_Ok);
            ShowBTN2("Xóa", ImageNameDelete);
            ShowBTN3("In hóa đơn", ImageNameEdit);
        }
        private void frmSell_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
        }
        public void ReSetForm() {
            txtProduct.Text = "";
            txtWShell.Text = "";
            txtMaterial.Text = "";
            txtPriceShell.Text = "";
            txtPriceUnit.Text = "";
          
            txtPriceL.Text ="";
          
    
            txtPriceM.Text ="";
  
            txtPriceProduct.Text ="";
            txtPriceUnit.Text = "";
  
        }
        
        private void LoadText(DataTable data) {
            if (!bool.Parse(data.Rows[0]["Change"].ToString()))
            {
                txtPriceUnit.Enabled = true;
                txtPriceShell.Enabled = false;
                txtPriceUnit.Text =utcommand.FormatMoney(data.Rows[0]["PriceUnit"].ToString().Trim(),"VND");
                float price=float.Parse(data.Rows[0]["PriceUnit"].ToString().Trim()) * float.Parse(data.Rows[0]["Weight"].ToString());
                txtPriceShell.Text = utcommand.FormatMoney(String.Format("{0:0.00}", price),"VND"); 
            }
            else {
                txtPriceUnit.Enabled = false;
                txtPriceUnit.Text = "<Giá cố định>";
                txtPriceShell.Enabled = true;
                txtPriceShell.Text = utcommand.FormatMoney(data.Rows[0]["Price"].ToString().Trim(), "VND");
            }

                txtProduct.Text = data.Rows[0]["Name"].ToString().Trim();
                txtWShell.Text = data.Rows[0]["Weight"].ToString().Trim();
                txtMaterial.Text = data.Rows[0]["GoldType"].ToString().Trim();
                LoadSeed(int.Parse(data.Rows[0]["ID"].ToString()));

                //txtPriceShell.Text = data.Rows[0]["Price"].ToString();
               // txtWL.Text = data.Rows[0]["SizeL"].ToString().Trim();
                //txtPriceL.Text =utcommand.FormatMoney(data.Rows[0]["PriceL"].ToString().Trim(),"VND");
               // txtNumL.Text = data.Rows[0]["NumberSeedLarge"].ToString().Trim();

               // txtWM.Text = data.Rows[0]["SizeM"].ToString().Trim();
              //  txtPriceM.Text =utcommand.FormatMoney(data.Rows[0]["PriceM"].ToString().Trim(),"VND");
              //  txtNumM.Text = data.Rows[0]["NumberSeedSmall"].ToString();
           //  float priceP = float.Parse(utcommand.FormatString(txtPriceShell.Text)) + float.Parse(utcommand.FormatString(txtPriceL.Text)) * int.Parse(txtNumL.Text) + float.Parse(utcommand.FormatString(txtPriceM.Text)) * int.Parse(txtNumM.Text);
            txtPriceProduct.Text = utcommand.FormatMoney(String.Format("{0:0.00}", float.Parse(utcommand.FormatString(txtPriceShell.Text)) +float.Parse(utcommand.FormatString(txtPriceL.Text)) + float.Parse(utcommand.FormatString(txtPriceM.Text))),"VND");

        }
        public string FormatWeight(string str)
        {
            string size = "";
            string[] tam = str.Trim().Split('.');
            string phanle;
            if (tam.Length > 1) phanle = tam[1];
            else phanle = "";

            switch (tam[0].Length)
            {
                case 1:
                    size = tam[0] + "L" + phanle;
                    break;
                case 2:
                    size = tam[0].Substring(0, 1) + "P"
                        + tam[0].Substring(1, 1) + "L" + phanle;
                    break;
                case 3:
                    size = tam[0].Substring(0, 1) + "C"
                        + tam[0].Substring(1, 1) + "P" +
                        tam[0].Substring(2, 1) + "L" + phanle;
                    break;
                case 4:
                    size = tam[0].Substring(0, 1) + "L" +
                        tam[0].Substring(1, 1) + "C"
                        + tam[0].Substring(2, 1) + "P" +
                        tam[0].Substring(3, 1) + "L" + phanle;
                    break;

            }
            return size;
        } 
        public void LoadSeed( int id) {
            DataTable dataSeed = tSell.SelectSeedByProduct(id);
            float priceM = 0;
            float priceL = 0;
            lvSeedL.Items.Clear();
            lvSeedM.Items.Clear(); 
            for (int j = 0; j < dataSeed.Rows.Count; j++)
            {
                if (dataSeed.Rows[j]["Name"].ToString().Trim() == "Hạt Tấm")
                {
                   
                    string size = "";
                        size = FormatWeight(dataSeed.Rows[j]["Size"].ToString());
                        ListViewItem item = new ListViewItem(dataSeed.Rows[j]["Type"].ToString() + ":" + size);
                        lvSeedM.Items.Add(item);
                        // item.SubItems.Add(stt.ToString());
                        item.SubItems.Add(dataSeed.Rows[j]["Num"].ToString());
                        priceM += float.Parse(dataSeed.Rows[j]["Price"].ToString()) * int.Parse(dataSeed.Rows[j]["Num"].ToString());
              }
                else
                {
                    string size = "";
                    size = FormatWeight(dataSeed.Rows[j]["Size"].ToString());
                    ListViewItem item = new ListViewItem(dataSeed.Rows[j]["Type"].ToString() + ":" + size);
                    lvSeedL.Items.Add(item);
                    // item.SubItems.Add(stt.ToString());
                    item.SubItems.Add(dataSeed.Rows[j]["Num"].ToString());
                priceL += float.Parse(dataSeed.Rows[j]["Price"].ToString()) * int.Parse(dataSeed.Rows[j]["Num"].ToString());
                
                }
            }
            txtPriceM.Text = utcommand.FormatMoney(priceM.ToString(),"VND");
            txtPriceL.Text = utcommand.FormatMoney(priceL.ToString(), "VND");

        }
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            if (txtBarcode.Text.Trim().Length  < 10) ReSetForm();
            else if (txtBarcode.Text.Trim().Length == 10) {
                dataPro = tSell.LoadProduct(txtBarcode.Text.Trim());
                if (dataPro.Rows.Count > 0)
                    if(dataPro.Rows[0]["Status"].ToString().Trim()=="1")
                {
                    ReSetForm();
                    LoadText(dataPro);
                }
            }

        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            //lvProduct.Items.Clear();
            int flag=0;
            int i =0;
            for (i= 0; i < lvProduct.Items.Count; i++)
                if (dataPro.Rows[0]["Barcode"].ToString().Trim() == lvProduct.Items[i].SubItems[1].Text.ToString().ToUpper().Trim())
                { 
                    flag = 1;
                    break;
                }
            if (flag == 0)
            {
              //  NumPro++;
                i++;
                ListViewItem item = new ListViewItem(i.ToString());
                lvProduct.Items.Add(item);

                item.SubItems.Add(txtBarcode.Text);

                item.SubItems.Add(txtProduct.Text.ToString());
                float price = float.Parse(utcommand.FormatString(txtPriceProduct.Text.ToString()));
                item.SubItems.Add(utcommand.FormatMoney(String.Format("{0:0.00}", price), "VND"));
                txtTotalPrice.Text = utcommand.FormatMoney(String.Format("{0:0.00}",(float.Parse(txtTotalPrice.Text) + price)), "VND");
                  if (i % 2 == 0)
                {
                   item.BackColor = Color.AliceBlue;
                    //return ;
                }
                else
                  {
                      item.BackColor = Color.AntiqueWhite;
                      //return Brushes.Beige;
                  }

            }
        }

        public infoTally_out SetTally() {
            infoTally_out info = new infoTally_out();
            info.DateCreate = DateTime.Now;
           
             //MainProject.MainMDIPaarent formmain = new MainProject.MainMDIPaarent();
            //UTLBASEFORM.MDIPARENT d = new UTLBASEFORM.MDIPARENT();

            info.MemberID = UserLoginInfo.ID;
            info.CustomerID = 1;
            info.NumberProduct = lvProduct.Items.Count;
            info.MoneySum = float.Parse(utcommand.FormatString(txtTotalPrice.Text));
            info.Active = true;
            return info;
        }
        private int InsertTally_out() {
            int id = 0;
            infoTally_out  info= SetTally();
            id= tSell.InsertTally(info);
            return id;

        }
        public void InsertDetail(int idTally) {
            infoDetailTally info;
            for (int i = 0; i < lvProduct.Items.Count; i++)
            {
                info = new infoDetailTally();
                info.TallyOutid = idTally;
                info.Barcode = lvProduct.Items[i].SubItems[1].Text.ToString();
                info.Price = float.Parse(utcommand.FormatString(lvProduct.Items[i].SubItems[3].Text.ToString()));
                tSell.InsertDetailTally(info);
                tSell.UpdateProduct(info.Barcode, 2);
            }
           // return tSell.InsertDetailTally(info);


        }


        public override void btn1_Click(object sender, EventArgs e)
        {
            int id = InsertTally_out();
            InsertDetail(id);
            ReSetForm();
            lvProduct.Items.Clear();
            txtTotalPrice.Text = "";
            lvSeedL.Items.Clear();
            lvSeedM.Items.Clear();
        }
        public override void btn2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvProduct.Items.Count; i++)
                if (lvProduct.Items[i].Checked == true)
                {
                    //I show here the second field text (SubItems[1].Text) from the selected row(Items[i]) 
                   // MessageBox.Show(lvEmployee.Items[i].SubItems[2].Text);
                  
                    float price =float.Parse(lvProduct.Items[i].SubItems[3].Text);
                    txtTotalPrice.Text = utcommand.FormatMoney(String.Format("{0:0.00}",(float.Parse(txtTotalPrice.Text) - price)), "VND");
                    lvProduct.Items[i].Remove();
                    // break;
                }
        }


      

      

      

       
    }
}